
import mixin from '@/service/mixin';
import Pagination from '@/components/pagination';

let that = null;
export default {
    name: 'smsManage-sendRecord',
    components: {
        Pagination,
    },
    mixins: [mixin],
    data() {
        return {
            domain:"",
            form: {
                phone: '',
                registerTime: [],
                sendProvider: '',
                sendStatus: '',
                supplier: '',
                callbackStatus: '',
            },
            autoSend: true,
            rules: {
                phone: [
                    {pattern: /^\d+$/, message: this.$t('verify.plsInputNumber')},
                ],
            },
            tableData: [],
            messageTypeData: [],
            autoMessageTypeData: [],
        };
    },
    methods: {
        // 搜索列表数据
        search() {
            const form = this.form;
            if (this.autoSend) {  //自动发送
                if((!form.registerTime || form.registerTime.length <= 0) && !form.phone && !form.supplier && !form.callbackStatus) {
                    this.$message.error(this.$tc('verify.plsCheckSearch'));
                    return;
                }
            }else {  //手动发送
                if((!form.registerTime || form.registerTime.length <= 0) && !form.phone && !form.sendProvider && !form.sendStatus) {
                    this.$message.error(this.$tc('verify.plsCheckSearch'));
                    return;
                }
            }
            this.$refs.form.validate(val => {
                if (val) {
                    this.page.currentPage = 1;
                    this.getTableData();
                }
            });
        },
        // 获取列表数据
        getTableData(page = this.page) {
            const form = this.form;
            const params = {
                startTime: '',
                endTime: '',
                phone: form.phone,
                pageNo: page.currentPage,
                pageSize: this.page.pageSize
            };
            if (this.autoSend) {
                params.supplier = form.supplier;
                params.callbackStatus = form.callbackStatus;
            }else {
                params.sendProvider = form.sendProvider;
                params.sendStatus = form.sendStatus;
            }
            if (form.registerTime && form.registerTime.length) {
                params.startTime = form.registerTime[0];
                params.endTime = form.registerTime[1];
            }
            this.$api.sms[this.autoSend ? 'selectSmsRecordByParams' : 'selectSmsBatchRecordDetailByParams'](params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    this.tableData = data.list;
                    this.setPage({total: data.total,page: page.currentPage}, this.page);
                }
                this.tableMaxHeight = document.body.clientHeight - 190;
            });
        },
        // getManualMessageType() {
        //     this.$api.sms.getOpenBatchSendProvider({}).then(res => {
        //         const {status, data, error, msg} = res;
        //         if ('1' === status && '00000000' === error) {
        //             this.messageTypeData = data;
        //         }
        //     });
        // },
        // getAutoMessageType() {
        //     this.$api.sms.getSmsSendAllProvider({}).then(res => {
        //         const {status, data, error, msg} = res;
        //         if ('1' === status && '00000000' === error) {
        //             this.autoMessageTypeData = data;
        //         }
        //     });
        // },
        changeSendType() {
            this.form.phone = '';
            this.form.registerTime = [];
            this.form.sendProvider = '';
            this.form.sendStatus = '';
            this.form.supplier = '';
            this.form.callbackStatus = '';
            this.page.pageNo = 1;
            this.page.recordsTotal = 0;
            this.tableData = [];
            this.autoSend = !this.autoSend;
        }
    },
    filters: {
        switchStatus(val) {
            switch (val + '') {
                case '0':
                    return that.$t('common.sending');// 正在发送
                case '1':
                    return that.$t('common.sendSuccess');// 发送成功
                case '2':
                    return that.$t('common.sendFaild');// 发送失败
                default:
                    return val
            }
        }
    },
    created() {
        that = this;
        this.domain = this.$storage.get('user').domain;
        // this.getManualMessageType();
        // this.getAutoMessageType();
        //this.getTableData();
    },
};
